//
//  hotTripController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "hotTripController.h"
#import "CustomTripCell.h"
#import "TripModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "hotDetailController.h"
#import "detaiTriplModel.h"

@interface hotTripController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation hotTripController

- (void)dealloc
{
    [_time release];
    [_array release];
    [_tabel release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.count1 = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor cyanColor]];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setTitle:@"热门游记"];
    [self createTable];
    [self startAsync];
    [self setupRefresh];
   
}


#pragma mark - 
#pragma mark  创建TableView
- (void)createTable
{
    self.tabel = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [_tabel setRowHeight:208];
    [_tabel registerClass:[CustomTripCell class] forCellReuseIdentifier:@"ok"];
    _tabel.delegate = self;
    _tabel.dataSource = self;
    [self.view addSubview:_tabel];
    [_tabel release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTripCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
    TripModel *model = [_array objectAtIndex:indexPath.row];
    [cell.nameLabel setText:model.name];
    NSNumber *num = [NSNumber numberWithInteger:model.day_count];
    [cell.dayLabel setText:[NSString stringWithFormat:@"%@", num]];
    NSNumber *num1 = [NSNumber numberWithInteger:model.view_count];
    [cell.countLabel setText:[NSString stringWithFormat:@"%@", num1]];
    [cell.placeLabel setText:model.popular_place_str];
    [cell.image setImageWithURL:[NSURL URLWithString:model.cover_image]];
    [cell.timeLabel setText:self.time];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    hotDetailController *detail = [[hotDetailController alloc] init];
    detaiTriplModel *model =  [_array objectAtIndex:indexPath.row];
    detail.numid = model.id;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

#pragma mark - 
#pragma mark 数据解析
- (void)startAsync
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/hot/?start=%ld&count=10&is_ipad=true", _count1]];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *array = [dic objectForKey:@"trips"];
        for (NSMutableDictionary *dic1 in array) {
            TripModel *model = [[TripModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [_array addObject:model];
            [_tabel reloadData];
            [model release];
        }
    }];

}



#pragma mark - 
#pragma mark - 加载。刷新
- (void)setupRefresh
{
    
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tabel addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    [self.tabel headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tabel addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tabel.headerPullToRefreshText = @"下拉可以刷新了";
    self.tabel.headerReleaseToRefreshText = @"松开立即刷新了";
    self.tabel.headerRefreshingText = @"正在刷新中";
    
    // self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tabel.footerReleaseToRefreshText = @"松开立即加载更多数据";
    self.tabel.footerRefreshingText = @"正在加载中";
}

- (void)headerRereshing
{
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self.tabel reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tabel headerEndRefreshing];
    });
}


- (void)footerRereshing
{
    
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self startAsync];
        [self.tabel reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tabel footerEndRefreshing];
    });
    
    _count1++;
    
}










@end
